#include <sourcemod>
#include <sdkhooks>

#define NAMELEN 20

public Plugin myinfo = 
{
	name = "BS Kills",
	author = "那一世的悲伤",
	description = "击杀统计",
	version = SOURCEMOD_VERSION,
	url = "http://www.sourcemod.net/"
};


int killSpecial[MAXPLAYERS+1];
int shotFriend[MAXPLAYERS+1];

public void OnPluginStart()
{
    RegConsoleCmd("sm_kills",OnKills,"显示击杀统计");
  	HookEvent("player_death", OnPlayerDeath);
    HookEvent("player_hurt", OnPlayerHurt);
	HookEvent("witch_killed", 	OnWitchKilled);
	HookEvent("tank_killed", 	OnTankKilled);
    HookEvent("round_start", OnRoundStart);
	HookEvent("round_end", OnRoundEnd);
}

bool IsValidClient(int client)
{
	return (client > 0 && client <= MaxClients && IsClientInGame(client));
}
//重置计数
void ResetCounter(int client)
{
  	killSpecial[client] = 0;
    shotFriend[client]=0;
}


//玩家连接
public void OnClientConnected(int client)
{   
	if(!IsFakeClient(client))
	{
	  	ResetCounter(client);
	}
}

//玩家离开.
public void OnClientDisconnect(int client)
{   
	if(!IsFakeClient(client))
	{
		ResetCounter(client);
	}
}

//回合开始.
public void  OnRoundStart(Event event, const char[] name, bool dontBroadcast)
{
  	for (int i = 1; i <= MaxClients; i++)
	{ 
		if (IsClientConnected(i) && IsClientInGame(i) && !IsFakeClient(i))
		{
			ResetCounter(i);
		}
	}
}

//回合结束 
public void OnRoundEnd(Event event, const char[] name, bool dontBroadcast)
{
    DisplayKills(0);
}

public void OnPlayerHurt(Event event, const char[] name, bool dontBroadcast)
{
  	int client = GetClientOfUserId(event.GetInt("userid")); //被攻击
	int attacker = GetClientOfUserId(event.GetInt("attacker")); //攻击方
	if( IsValidClient(client) && IsValidClient(attacker)) 
	{
		//记录黑枪
		if(GetClientTeam(attacker) == GetClientTeam(client) && GetClientTeam(client) != 3 && attacker != client)
		{
			shotFriend[attacker] += GetEventInt(event, "dmg_health");
		}
	}
}

public void OnWitchKilled(Event event, const char[] name, bool dontBroadcast)
{
	int attacker = GetClientOfUserId(event.GetInt("userid"));
	ShowMessage(0,"\x03Witch死于\x04%N\x03之手",attacker);
}

public void OnTankKilled(Event event, const char[] name, bool dontBroadcast)
{
	int attacker = GetClientOfUserId(event.GetInt("attacker"));
	ShowMessage(0,"\x03Tank死于\x04%N\x03之手",attacker);
}

public void OnPlayerDeath(Event event, const char[] name, bool dontBroadcast)
{
  int client = GetClientOfUserId(event.GetInt("userid"));
  int attacker = GetClientOfUserId(event.GetInt("attacker"));
  if(IsValidClient(client) && GetClientTeam(client) == 3)
	{
		if(IsValidClient(attacker) && GetClientTeam(attacker) == 2)
		{
      		killSpecial[attacker] += 1;
      		int iClass= GetEntProp(client, Prop_Send, "m_zombieClass")
			switch(iClass)
			{
				case 1: PrintToChat(attacker,"\x03击杀%s","smoker");
				case 2: PrintToChat(attacker,"\x03击杀%s","boomer");
				case 3: PrintToChat(attacker,"\x03击杀%s","hunter");
				case 4: PrintToChat(attacker,"\x03击杀%s","spitter");
				case 5: PrintToChat(attacker,"\x03击杀%s","jockey");
				case 6: PrintToChat(attacker,"\x03击杀%s","charger");
			}
		}
	}
}

public int SortByDamageDesc(int elem1, int elem2, const char[] array, Handle hndl)
{
	 return killSpecial[elem2] - killSpecial[elem1];
}

void ShowMessage(int client,char[] format,any ...)
{
	char buffer[254];
 	if(client>0&&IsClientInGame(client))
	{
		SetGlobalTransTarget(client);
		VFormat(buffer, sizeof(buffer), format, 3);
		PrintToChat(client, "%s", buffer);
	}else
	{
		for (int i = 1; i <= MaxClients; i++)
		{
			if (IsClientInGame(i))
			{
				SetGlobalTransTarget(i);
				VFormat(buffer, sizeof(buffer), format, 3);
				PrintToChat(i, "%s", buffer);
			}
		}
	}
}

void DisplayKills(int clientId)
{
  	int playerCount;
    int players[MAXPLAYERS+1];
    for (int client = 1; client <= MaxClients; client++)
    {
      if (IsClientInGame(client) && GetClientTeam(client) == 2)
      {
        players[playerCount++] = client;
      }
    }
    SortCustom1D(players, playerCount, SortByDamageDesc);
	ShowMessage(clientId,"\x04-----------击杀排行-----------");
    for (int i = 0; i < playerCount; i++)
    {
      int client=players[i];
      int zombieKills=  GetEntProp(client, Prop_Send, "m_checkpointZombieKills");
	  char line[128];
	  Format(line, sizeof(line),"\x04☆%d \x05特感:\x04%d\x03 僵尸:\x04%d\x03 黑枪:\x05%d\x03 \x04-%N",i+1, killSpecial[client],zombieKills,shotFriend[client],client);
	  ShowMessage(clientId,line);
    }
}

public Action OnKills(int client, int args)
{
  	DisplayKills(client);
}